package com.example.easyscript.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class RobotUtils {

    static {
        try {
            robotInstance = new Robot() ;
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    private static Robot robotInstance ;

    /**
     * 获取截图
     * @param rectangle 矩形
     * @param stockPath 地址
     */
    public static void getCapture(Rectangle rectangle,String stockPath)  {
        try {
            BufferedImage screenCapture = robotInstance.createScreenCapture(rectangle);
            File file = new File(stockPath) ;
            ImageIO.write(screenCapture,"png",file ) ;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 移动鼠标到对应位置
     */
    public static void moveMouse(Point point){
        robotInstance.mouseMove(point.x, point.y);
    }

    /**
     * 单击操作
     */
    public static void click(long time){
        robotInstance.mousePress(InputEvent.BUTTON1_MASK);
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        robotInstance.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    /**
     * 单击某个地方
     */
    public static void clickPoint(Point point){
        moveMouse(point) ;
        click(400) ;
    }

}